package com.s.core.daily_import.remote;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.s.core.daily_import.Utils;
import com.s.core.daily_import.remote.vo.RemoteVo;
import com.s.core.daily_import.remote.vo.json.Bean;
import com.s.core.daily_import.remote.vo.json.F;
import com.s.core.daily_import.remote.vo.json1.Data;
import com.s.core.root.CodeFeature;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class Remote {
    private static final String TRADE_DATE_URL_PREFIX =
            "https://www.szse.cn/api/report/exchange/onepersistenthour/monthList?month=%s";
    private static final String DAILY_DATA_URL = "https://54.push2.eastmoney.com/api/qt/clist/get" +
            "?pn=%d" + //page no
            "&pz=100" +//page size
            "&po=1" + //正序
            "&fid=f3" +//排序字段,涨跌幅
            "&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048" +
            "&fields=f12,f14,f2,f15,f18,f5,f9";//返回字段:代码,名称,收盘价,最高价,昨收价,成交量,动态市盈率(0为已退市)

    public static String nextTradeDate(String date) {
        String recentDate = Utils.tranDate(date, "yyyyMMdd", "yyyy-MM-dd");
        String recentMonth = Utils.toMonth(recentDate);
        String nextMonth = Utils.nextMonth(recentMonth);

        com.s.core.daily_import.remote.vo.json1.Bean recentMonthBean = monthBean(recentMonth);
        com.s.core.daily_import.remote.vo.json1.Bean nextMonthBean = monthBean(nextMonth);

        List<Data> data = Lists.newArrayList();
        data.addAll(recentMonthBean.getData());
        data.addAll(nextMonthBean.getData());
        List<String> tradeDates = data.stream().filter(d -> d.getJybz().equals("1")).
                map(Data::getJyrq).collect(Collectors.toList());
        return tradeDates.get(tradeDates.indexOf(recentDate) + 1);
    }

    public static List<RemoteVo> dailyData() {
        List<RemoteVo> result = Lists.newArrayList();
        int pn = 1;
        while (true) {
            List<RemoteVo> vos = getRemote(pn);
            if (vos == null) {
                break;
            }
            result.addAll(vos);
//            System.out.println("result.size = " + result.size() + ",vos.size=" + vos.size() + ",pn=" + pn);
            pn++;
        }
        return result;
    }

    private static com.s.core.daily_import.remote.vo.json1.Bean monthBean(String month) {
        String url = String.format(TRADE_DATE_URL_PREFIX, month);
        String content = Utils.httpGet(url);
        return new Gson().fromJson(content, com.s.core.daily_import.remote.vo.json1.Bean.class);
    }

    private static List<RemoteVo> getRemote(int pn) {
        String content = Utils.httpGet(String.format(DAILY_DATA_URL, pn));
        Bean bean = new Gson().fromJson(content, Bean.class);
        if (bean == null || bean.getData() == null || bean.getData().getDiff() == null) {
            return null;
        }
        List<RemoteVo> voList = Lists.newArrayList();
        for (int i = 0; i < bean.getData().getDiff().size(); i++) {
            F f = bean.getData().getDiff().get(String.valueOf(i));
            if (f.getF9() == 0 || CodeFeature.isSt(f.getF14()) || CodeFeature.isToDel(f.getF14())) {
                continue;
            }
            voList.add(new RemoteVo(f.getF12(), f.getF14(), f.getF2(), f.getF15(), f.getF18(), f.getF5()));
        }
        return voList;
    }
}
